package com.xywl.escort.view.splash

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.BannerResp
import com.xywl.escort.common.PreferenceKeys.IS_FIRST
import com.xywl.escort.utils.RxHelper
import com.xywl.escort.utils.RxSPTool
import com.xywl.escort.widget.GlideApp
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_splash.*
import org.reactivestreams.Subscriber
import org.reactivestreams.Subscription
import timber.log.Timber
import javax.inject.Inject

@Route(path = "/xy/splash")
class SplashActivity : BaseActivity(), SplashContract.View {
    private var mIsSkip = false
    private var mDataBean: BannerResp.DataBean? = null

    @Inject
    lateinit var mPresenter: SplashContract.Presenter

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        updateViews()
        mPresenter.takeView(this)
        mPresenter.getAdvert()

        btn_skip.setOnClickListener {
            onDoSkip()
        }
    }

    private fun updateViews() {
        RxHelper.countdown(3)
                .compose(this.bindToLifecycle<Int>())
                .subscribe(object : Subscriber<Int> {

                    override fun onSubscribe(s: Subscription) {
                        s.request(java.lang.Long.MAX_VALUE)
                    }

                    override fun onNext(integer: Int?) {
                        val skip = "跳过 $integer"
                        btn_skip.text = skip
                    }

                    override fun onError(e: Throwable) {
                        onDoSkip()
                    }

                    override fun onComplete() {
                        onDoSkip()
                    }
                })
    }

    private fun onDoSkip() {
        if (!mIsSkip) {
            mIsSkip = true

            val isFirst = RxSPTool.getBoolean(this, IS_FIRST)
            if (!isFirst) {
                ARouter.getInstance().build("/xy/guide").navigation()
                finish()
            } else {
                ARouter.getInstance().build("/xy/main").navigation()
                finish()
            }
        }
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T : Any?> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun getAdvertSuccess(dataBean: BannerResp.DataBean?) {
        mDataBean = dataBean
        processAD()
    }

    override fun getAdvertFailure() {
        Timber.e("getAdvertFailure")
    }

    private fun processAD() {
        if (mDataBean == null) {
            return
        }
        GlideApp.with(this)
                .load(mDataBean!!.imgUrl)
                .centerCrop()
                .placeholder(R.mipmap.icon_default_picture)
                .into(image_splash)
    }

    override fun onDestroy() {
        super.onDestroy()

        mDataBean = null
    }

    override fun onBackPressed() {
        return
    }
}
